#!/usr/bin/perl
#
# Produce a graph of opcounts
#
use Counter;

my $stats = Counter::openfile("opcounts");

$name = "persons";
$qualifier = "";
$period = "month";

my @oplist = $stats->selectany("distinct operation from opcounts");
my @statlist = $stats->select("name=? AND qualifier=? AND period=?
	       ORDER BY operation, date", $name, $qualifier, $period);


#open(GNUPLOT, "|gnuplot");
open(GNUPLOT, ">-");
print GNUPLOT <<EoF;
set xdata time
set timefmt "%Y-%m-%d%H:%M:%S"
set xlabel "Year"
set format x "%Y"
set terminal png
set key top left
EoF
print GNUPLOT "plot ";
$first = 1;
for $opname (@oplist) {
    my $operation = $opname->{operation};
    print GNUPLOT ", " if !$first;
    print GNUPLOT "'-' using 1:2 title \"$operation\" with lines";
    $first = 0;
}
print "\n\n";


for $stat (@statlist) {
    $stat->{date} =~ s/ /T/;
    if ($prevop && $stat->{operation} ne $prevop) {
	print GNUPLOT "end\n";
    }
    $prevop = $stat->{operation};
    print GNUPLOT "$stat->{date}\t$stat->{count}\t$stat->{operation}\n";
}
print "end\n";

